﻿using Demo.Data;
using Demo.Models;
using Microsoft.EntityFrameworkCore;
//WebApplication为整个Web应用程序

//CreateBuilder()方法创建一个WebApplicationBuilder 对象
var builder = WebApplication.CreateBuilder(args);

//注册Razor Pages服务
// Add services to the container.
builder.Services.AddRazorPages();


//将DemoContext上下文对象注入到应用中
builder.Services.AddDbContext<DemoContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DemoContext") 
    ?? throw new InvalidOperationException("Connection string 'DemoContext' not found.")));


//构造一个 WebApplication 实例
var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
    SeedData.Initialize(scope.ServiceProvider);
}

//app.Environment用来获取当前Web应用程序的环境变量
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
}

//启用静态文件中间件，加上这行代码后， 我们在浏览器中才能访问 wwwroot 目录下的文件，否则会报错
app.UseStaticFiles();

//启用路由中间件
app.UseRouting();

//启用权限验证中间件，当我们的网站是基于身份认证的话需要用到
app.UseAuthorization();

//向 IEndpointRouteBuilder 添加 Razor Pages 的终结点
app.MapRazorPages();

//启动web实例
app.Run();
